package com.bao.Servlet;

import com.bao.Dao.UserDao;
import com.bao.DaoImpl.UserDaoImpl;
import com.bao.Emtity.User;
import com.bao.Emtity.agency;
import com.bao.Service.AgencyService;
import com.bao.Service.RegionService;
import com.bao.Service.UserService;
import com.bao.ServiceImpl.AgencyServiceImpl;
import com.bao.ServiceImpl.RegionServiceImpl;
import com.bao.ServiceImpl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by TangBaoshan on 2018/8/11.
 */
@WebServlet(name = "AddinfoServlet")
public class AddinfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        int count = 0;
        int aff = 0;
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        User u = new User();
        agency a = new agency();
        UserDao use = new UserDaoImpl();
        int drank = 0;//代理级别
        UserService userse = new UserServiceImpl();
        RegionService se = new RegionServiceImpl();
        AgencyService ase = new AgencyServiceImpl();
        u.setOpenid(request.getParameter("openid"));
        u.setUname(request.getParameter("uname"));
        u.setUphone(request.getParameter("phone"));
        u.setUpassword(request.getParameter("pwds"));
        String pro = request.getParameter("province") + "省";
        u.setUprovince(se.findregionid(pro));
        u.setUcity(se.findregionid(request.getParameter("city")));
        u.setUcounty(se.findregionid(request.getParameter("area")));
        u.setUaddress(request.getParameter("address"));
        u.setUsex(request.getParameter("usex"));
        u.setUtype(4);
        u.setCheck(1);
        u.setUimage("default");
        u.setUemail(request.getParameter("email"));
        int num = userse.AddUser(u);
        int findid = userse.Findid(u.getUname());
        a.setUid(findid);
        if (request.getParameter("city").equals("市辖区") && request.getParameter("area").equals("市辖区")) {
            aff = se.findregionid(request.getParameter("province") + "省");
            drank = 1;
        } else if (!request.getParameter("city").equals("市辖区") && request.getParameter("area").equals("市辖区")) {
            aff = se.findregionid(request.getParameter("city"));
            drank = 2;
        } else if (!request.getParameter("city").equals("市辖区") && !request.getParameter("area").equals("市辖区")) {
            String n = request.getParameter("area");
            aff = se.findregionid(n);
            drank = 3;
        }
        a.setDrank(drank);
        a.setAffiliating(aff);
        count = ase.Addagency(a);
        int i = count + num;
        if (i > 1) {
            //out.print("<script language='javascript'>parent.layer.close(parent.layer.getFrameIndex(window.name));</script>");
            out.print(i);
        } else {
            out.print(i);
            out.print("<script language='javascript'>history.go(-1);alert('新增失败');</script>");
        }
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
